<?php
class Input {
    private $data;
    
    const INT = 'int';
    const FLOAT = 'float';
    const HTML = 'html';
    const RAW = 'raw';
    
    public function __construct($data = false) {
        if (!$data) $this->data = array_merge($_GET,$_POST);
        else {
            assert('is_array($data) /* need to provide an array */');
            $this->data = $data;
        }
    }
    
    public function get($field,$format = 'raw',$default = null) {
        if ( isset($this->data[$field]) ) {
            $fcn = "clean_$format";
            switch ($format) {
                case 'raw':
                    return $this->data[$field];
                    break;
                case 'int':
                case 'float':
                case 'html':
                    return $this->$fcn($this->data[$field]);
                    break;
                default:
                    throw new Exception("Unfamiliar data type: $format");
                    break;
            }
        } else {
            return $default;
        }
    }
    
    private function clean_int($val) { return (int) $val; }
    private function clean_float($val) { return (float) $val; }
    private function clean_html($val) { return htmlspecialchars($val); }

}
?>